home *** CD-ROM | disk | FTP | other *** search
/ IRIX Installation Tools & Overlays 2002 November / SGI IRIX Installation Tools & Overlays 2002 November - Disc 4.iso / dist / infosearch.idb / usr / lib / infosearch / bin / man2html.pl.z / man2html.pl
Text File  |  2002-10-15  |  12KB  |  473 lines

  1. #
  2. # man2html.pl
  3. #
  4. # Copyright 1996-2002, Silicon Graphics, Inc.
  5. # All Rights Reserved.
  6. #
  7. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  8. # the contents of this file may not be disclosed to third parties, copied or
  9. # duplicated in any form, in whole or in part, without the prior written
  10. # permission of Silicon Graphics, Inc.
  11. #
  12. # RESTRICTED RIGHTS LEGEND:
  13. # Use, duplication or disclosure by the Government is subject to restrictions
  14. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  15. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  16. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  17. # rights reserved under the Copyright Laws of the United States.
  18. #
  19. # requires perl5 and format2html.pl
  20. #
  21.  
  22. require 'format2html.pl';
  23.  
  24.  
  25. #######################################################################
  26. #
  27. # void man2html($root, $fname, $srch_str)
  28. #
  29. #######################################################################
  30.  
  31. sub man2html {
  32.  
  33.     my($manroot, $in_fname, $in_srch) = @_;
  34.  
  35.     if( $in_fname eq '' ) {
  36.         print "man2html: filename not specified" if($_WEB == 0);
  37.         return;
  38.     }
  39.  
  40.     my($mancmd,$command,$section) = "";
  41.     my($redirectpath)= "$manroot/usr/share/catman:" .
  42.                        "$manroot/usr/share/man:$manroot/usr/catman:" .
  43.                        "$manroot/usr/man";
  44.     $ENV{'MANPATH'}=$redirectpath;
  45.  
  46.     &buildExpression($in_srch);
  47.  
  48.     my(@fname) = split(/\ /, $in_fname);
  49.     if ($#fname == 0){
  50.  
  51.         if ($fname[0] =~ /\.gz$/){
  52.  
  53.             if($manroot && $fname[0] !~ /^$manroot/){
  54.                &outputHTMLPage("$manroot/$fname[0]");
  55.             }else{
  56.                &outputHTMLPage($fname[0]);
  57.             }
  58.             return;
  59.  
  60.         }elsif($fname[0] =~ /\.(z|[1-9][a-zA-Z]*|Z)$/){
  61.  
  62.             # 3 cases:
  63.             # 1) file is absolute /usr/share/catman/man1/test.z
  64.             # 2) file is relative usr/share/catman/man1/test.z
  65.             # 3) file is relative to the current location
  66.             # 
  67.             if($manroot){
  68.                $mancmd = "/usr/bin/man -c -d $manroot/$fname[0] 2< /dev/null |";
  69.             }else{
  70.                $mancmd = "/usr/bin/man -c -d $fname[0] 2< /dev/null |";
  71.             }
  72.             &formatManPage($mancmd);
  73.             return;
  74.  
  75.         }else{  
  76.             # Find out how many man pages match
  77.             #
  78.             $command = $fname[0];
  79.             $mancmd="/usr/bin/man -p $command 2< /dev/null |";
  80.         }
  81.  
  82.     }elsif($#fname == 1 && $fname[0] =~ /[0-8]/){
  83.  
  84.         $section = $fname[0];
  85.         $command = $fname[1];
  86.         $mancmd="/usr/bin/man -p $section $command 2< /dev/null |";
  87.  
  88.     }else{
  89.         print "man2html: incorrect filename specified" if($_WEB == 0);
  90.         return;
  91.     }
  92.  
  93.  
  94.     my(@manpagefiles) = &findMatches($mancmd);
  95.  
  96.     # No man pages found, but an inaccurate cmd given?
  97.     #
  98.     if($#manpagefiles == -1 && $command =~ /__/){
  99.  
  100.        my($tmp_command) = $command;
  101.        $tmp_command =~ s/__/_/g;
  102.        $mancmd = "/usr/bin/man -p $section $tmp_command |";
  103.        @manpagefiles = &findMatches($mancmd);
  104.     }
  105.  
  106.     # One man page found; display it and get out
  107.     #
  108.     if ($#manpagefiles == 0){
  109.  
  110.         if ($manpagefiles[0] =~ /\.gz$/){
  111.             &outputHTMLPage($manpagefiles[0]);
  112.         }else{
  113.             $mancmd = "/usr/bin/man -c -d $manpagefiles[0] 2< /dev/null |";
  114.             &formatManPage($mancmd);
  115.         }
  116.  
  117.         return;
  118.     }
  119.  
  120.     # Multiple man pages
  121.     #
  122.     if ($#manpagefiles > 0){
  123.  
  124.         &HTMLHeaderFormat("Matching Man Pages");
  125.         print '<B>Matching Man Pages</B>';
  126.         &multiPageIndex($manroot,@manpagefiles);
  127.         return;
  128.     }
  129.  
  130.     # At this point, no man pages were found
  131.     # Attempt fallback, use just cmd, not section id (if specified)
  132.     #
  133.     if ($section ne ''){
  134.  
  135.         $mancmd = "/usr/bin/man -p $command |";
  136.         my(@manpagefiles) = &findMatches($mancmd);
  137.  
  138.         if ($#manpagefiles > -1) {
  139.             &HTMLHeaderFormat("Matching Man Pages");
  140.             print '<B>Alternate Matching Man Pages</B>',
  141.                   '<p>Unable to find specific manual page for: ',
  142.                   "<b>${command}(${section})</b>; found these as ",
  143.                   "possible alternates:</p>\n";
  144.             &multiPageIndex($manroot,@manpagefiles);
  145.             return;
  146.         }
  147.     }
  148.  
  149.     &HTMLHeaderFormat("TPL");
  150.     print "Unable to find a manual page for: $command";
  151.     &HTMLTrailerFormat();
  152. }
  153.  
  154.  
  155. #######################################################################
  156. #
  157. # void formatManPage(String $mancmd)
  158. #
  159. # Uncompress the page and read it in one paragraph at a time.
  160. # Pass in the man cmd, as opposed to just the file, because we can 
  161. # tell if nroff exists on the system or not.
  162. #
  163. #######################################################################
  164.  
  165. sub formatManPage{
  166.  
  167.     my($mancmd) = @_;
  168.  
  169.     my($pgbrkcfmt) = "./pgbrkcfmt";
  170.     my($ss2nonss)  = "./ss2nonss";
  171.     my($manfilter) = "$pgbrkcfmt";
  172.     my($manfile)   = $mancmd;
  173.  
  174.     if($manfile =~ /\ ([^\ ]+\.[^\ ]+)\ /){
  175.     $manfile = $1;
  176.     unless(-e $manfile){
  177.         die "$manfile: $!\n";
  178.     }
  179.     }else{
  180.     die "Can't get filename: $mancmd\n";
  181.     }
  182.     
  183.     if( $manfile =~ /\.z$/ ) {                        # Packed
  184.     $mancmd = "/usr/bin/pcat $manfile |col |";
  185.     } elsif($manfile =~ /\.gz$/) {                  # Gzipped 
  186.     $mancmd = "/usr/sbin/gzip -dc $manfile | $ss2nonss |";
  187.     } elsif($manfile =~ /\.Z$/) {                # Compressed
  188.     $mancmd = "/usr/bsd/zcat $manfile |col |";
  189.     } elsif($manfile =~ /(\.[1-8].*)$/) {           # Unformatted troff
  190.         $mancmd = "/usr/bin/man -c -d $manfile | col -b |";
  191.     } else {
  192.     return;
  193.     }
  194.         
  195.     open(MANINPUT, "$mancmd  $manfilter |") || die "$mancmd $manfilter: $!";
  196.  
  197.     $/ = "\n\n+";
  198.  
  199.     my @paraArray = ();
  200.  
  201.     local($refName) = "";
  202.     local($refSection) = "";
  203.     local($refTitle) = "";
  204.     local($currentSectionName) = "";
  205.     local($foundTitle)   = 0;
  206.     local($foundHeader)  = 0;
  207.  
  208.     while(<MANINPUT>){
  209.  
  210.     if($_ eq ''){
  211.         next;
  212.     }
  213.  
  214.         $_ =~ s/\+\x8//g;
  215.     &pageBreakRemoval(\$_);
  216.  
  217.         if(/\n\n/){
  218.             @paraArray = split(/\n\n+/, $_);
  219.             foreach $para (@paraArray){
  220.                 &formatManParagraph(\$para);
  221.                 print "\n";
  222.             }
  223.         }else{
  224.             &formatManParagraph(\$_);
  225.         }
  226.     }
  227.     close(MANINPUT);
  228.  
  229.     &HTMLTrailerFormat();
  230.     return;
  231. }
  232.  
  233.  
  234. #######################################################################
  235. #
  236. # void formatManParagraph(String_ref \$buf)
  237. #
  238. # Identify a paragraph
  239. #
  240. #######################################################################
  241.  
  242. sub formatManParagraph{
  243.  
  244.     my($buf_ref) = @_;
  245.    
  246.     if($$buf_ref eq ''){
  247.     return;
  248.     }
  249.  
  250.     # Remove trailing page numbers
  251.     #
  252.     if($$buf_ref =~ /(<B>)?Page(<\/B>)?\ (<B>)?\d\d?(<\/B>)?/){
  253.         return;
  254.     }
  255.  
  256.     if($foundHeader==0){    
  257.  
  258.     if ($$buf_ref =~ /^(\ *)(\S+)/){  # Get the name from the header
  259.             # Missing a header - 1st para is NAME section
  260.         if($$buf_ref =~ /<B>NAME<\/B>\n\ +([^\ \,]+)/i){
  261.         $refName = $1;
  262.         $refSection="";
  263.         }else{
  264.         ($refName,$refSection) = &getHeaderInfo($2);
  265.         $foundHeader=1;
  266.         return;
  267.         }
  268.     }else{
  269.         $foundHeader=0; 
  270.     }
  271.     }    
  272.  
  273.     if($foundTitle==0){        # Glean out the man page's title
  274.  
  275.     if($$buf_ref =~ /<B>NAME<\/B>\n\ +(.+)/i){ # Match Name or Name
  276.         $refTitle = &getTitle($$buf_ref,$refSection);
  277.         &HTMLHeaderFormat($refTitle);
  278.         &bigFormat($refName);
  279.         $foundTitle=1;
  280.     }else{
  281.         $refTitle=$refName;
  282.         &HTMLHeaderFormat($refTitle);
  283.         &bigFormat($refName);
  284.         $foundTitle=1;
  285.     }
  286.     }
  287.     
  288.     #
  289.     # At this point we have some sort of body paragraph
  290.     # Apply links and keyword highlighting if necessary
  291.     #
  292.     &addLinks($buf_ref);
  293.     &hilite($buf_ref);
  294.  
  295.     # this is for the named sections (NAME, SYNOPSIS, etc)
  296.     #
  297.     if($$buf_ref =~ /^<B>/) {
  298.        $$buf_ref =~ s/^<B>/ <B>/;
  299.        $$buf_ref =~ s/\n/\n\n/;
  300.     }
  301.  
  302.     print $$buf_ref, "\n";
  303. }
  304.  
  305.  
  306. #######################################################################
  307. #
  308. # @manpagefiles findMatches(String $mancmd)
  309. #
  310. # Build a list of man pages which match the command.
  311. # For example, there my be multiple man pages named "test"
  312. #
  313. #######################################################################
  314.  
  315. sub findMatches{
  316.  
  317.     my($mancmd) = @_;
  318.     my(@manpagefiles) = ();
  319.  
  320.     open(MANPIPE, $mancmd) || die "$mancmd: !$\n";
  321.  
  322.     while(<MANPIPE>){
  323.     if(/^([^\/]+)(\/[\S]+)(\ *\|)(.+)/){
  324.        push(@manpagefiles, $2);
  325.     }
  326.     }
  327.     close(MANPIPE);
  328.     
  329.     return(@manpagefiles);
  330. }
  331.  
  332.  
  333. #######################################################################
  334. #
  335. # ($refName,$refSection) = getHeaderInfo(String $buf);
  336. #
  337. #######################################################################
  338.  
  339. sub getHeaderInfo{
  340.  
  341.     my($refName) = @_;
  342.     my($refSection) = "";
  343.  
  344.     &removeHTMLTags(\$refName);
  345.  
  346.     if($refName =~ /\((.+)\)/){
  347.     $refSection = $1;
  348.     }else{ # This could be a weird page like 2a_ctrl, which 
  349.        # has a header with a " " section: 2A_CTRL( )
  350.     $refName =~ s/\(//;
  351.     }
  352.  
  353.     return($refName,$refSection);
  354. }
  355.  
  356.  
  357. #######################################################################
  358. #
  359. # string getTitle(String $buf,String $refSection);
  360. #
  361. #######################################################################
  362.  
  363. sub getTitle{
  364.  
  365.     my($refTitle,$refSection) = @_;
  366.  
  367.     &removeHTMLTags(\$refTitle);
  368.     $refTitle =~ s/NAME\n\ *//i;
  369.     $refTitle =~ s/\n\ +/\ /g;
  370.     $refTitle =~ s/\n//g;
  371.     
  372.     # Format the title in makewhatis format (add section number)
  373.     if($refSection ne ""){
  374.     $refTitle=~s/(\ +\-)/\ \($refSection\)$1/;
  375.     }
  376.  
  377.     return($refTitle);
  378. }
  379.  
  380.  
  381. #######################################################################
  382. #
  383. # void multiPageIndex($manroot, Array @manpagefiles)
  384. #
  385. # When we have multiple man pages, create an index to a 
  386. # list of pages. Print index to stdout.
  387. #
  388. #######################################################################
  389.  
  390. sub multiPageIndex{
  391.  
  392.     my($manroot) = shift @_;
  393.     $manroot = quotemeta($manroot);
  394.  
  395.     my($script_name) = $ENV{SCRIPT_NAME} || "/cgi-bin/infosrch.cgi";
  396.     my($coll) = "";
  397.     $coll = (split(';', $COLLECTION))[0] if ($COLLECTION ne "");
  398.     my($cgi) = "$script_name\?cmd=getdoc&coll=$coll&db=man&fname=";
  399.     my($progType,$manPageName,$manPagePath,$useFileName) = "";
  400.  
  401.     my($bTable,$eTable) = "";
  402.     my($i) = 0;
  403.  
  404.     $bTable = "\n<TABLE CELLPADDING=0>\n";
  405.     $eTable = "</TABLE>\n";
  406.  
  407.     print $_ePreBody, $bTable;
  408.  
  409.     foreach $manPageFile (@_){
  410.  
  411.         $useFileName = $manPageFile;
  412.         if ($manroot ne ''){
  413.             $manPageFile =~ s/$manroot//;
  414.             $useFileName = $manPageFile;
  415.         }
  416.  
  417.         if($manPageFile =~ /(.+)\/(.+)\.z/o){
  418.  
  419.         $manPagePath = $1;
  420.         $manPageName = $2; 
  421.  
  422.            print "<TR>\n";
  423.  
  424.         $refType  = "Manual Reference Page";
  425.         $progType = '';
  426.         
  427.         if ($manPagePath =~ /a_man/){
  428.             $refType = "Administrative Reference Page";
  429.         }elsif ($manPagePath =~ /g_man/){
  430.             $refType = "Graphics Reference Page";
  431.         }elsif ($manPagePath =~ /u_man/){
  432.             $refType = "Users' Reference Page";
  433.         }elsif ($manPagePath =~ /p_man/){
  434.         
  435.             if($manPagePath =~ /ftn/ || $manPagePath =~ /f90/){
  436.            $progType="Fortran ";
  437.             }elsif($manPagePath =~ /cat3c/ || $manPagePath =~ /standard/){
  438.            $progType="C ";
  439.             }elsif($manPagePath =~ /cat3dm/ || $manPagePath =~ /dmedia/){
  440.            $progType="Digital Media ";
  441.             }elsif($manPagePath =~ /cat3b/){
  442.            $progType="BSD ";
  443.             }elsif($manPagePath =~ /cat3n/){
  444.            $progType="Networking ";
  445.             }elsif($manPagePath =~ /catD/){
  446.            $progType="Device Driver ";
  447.             }elsif($manPagePath =~ /perl/){
  448.            $progType="Perl ";
  449.             }elsif($manPagePath =~ /\/X/){
  450.            $progType="X11,Xt,etc. ";
  451.             }
  452.         
  453.             $refType = "Programmers' Reference Page";
  454.         }
  455.     
  456.             $useFileName = &escape($useFileName);
  457.  
  458.             print "<TD><A HREF=\"${cgi}${useFileName}\">",
  459.                   "${manPageName}</A></TD>\n<TD>",
  460.                   "    ",
  461.                   "${progType}${refType}</font></TD></TR>\n";
  462.         }
  463.     }
  464.     print $eTable, $_bPreBody;
  465.     &HTMLTrailerFormat();
  466. }
  467.  
  468.  
  469. # Return true from package include
  470. #
  471. 1;
  472.  
  473.